package cscie160.htest.client;
/**
 * this calss will be talking to ATM
*/

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.UnknownHostException;

public class Client  {
  public static void main(String[] args) {
    cscie160.htest.atm.ATM atm = null;
    int port = 1099;

    if (args.length > 0 && !(args[0].equals("?") || args[0].equalsIgnoreCase("help"))){
      try{
        port = Integer.parseInt(args[0]);
      } catch (NumberFormatException nfex){
        System.out.println("Provided argument\"" + args[0] + "\" can't be a port");
        return;
      }
    }
    try {
      /*
      ATMFactory factory = (ATMFactory)Naming.lookup("//localhost/atmfactory");
      System.out.println("Client.main(): factory=" +
        (factory==null ? "null" : factory.hashCode()) + " is gotten");
      atm = factory.getATM();
      */
      String atmLookupUrl = "//localhost:" + Integer.toString(port) + "/atm";
      atm = (cscie160.htest.atm.ATM)Naming.lookup(
atmLookupUrl);

      System.out.println("Client.main(): atm=" +
        (atm==null ? "null" : atm.hashCode()+"(hashCode)") +" is gotten");
      } catch (MalformedURLException mue) {
        mue.printStackTrace();
      } catch (NotBoundException nbe) {
        nbe.printStackTrace();
      } catch (UnknownHostException uhe) {
        uhe.printStackTrace();
      } catch (RemoteException re) {
        re.printStackTrace();
      }
      if (atm == null)
        return;
      
      byte byteVal = 5;
      if (args.length > 1 && !(args[0].equals("?") || args[0].equalsIgnoreCase("help"))){
        try{
          byteVal = Byte.parseByte(args[1]);
        } catch (NumberFormatException nfex){
          byteVal = -1;
        }
      }
      try {
        // get initial byteField
        System.out.println("Initial byteField of " + atm.getClass().getName() +
          "=" + atm.getByteField());
        atm.setByteField(byteVal);
        System.out.println("After setByteField(" + Byte.toString(byteVal) +
          ") byteField=" + atm.getByteField());
      } catch (RemoteException re) {
        System.out.println("An exception occurred while communicating with the ATM");
        re.printStackTrace();
      }
   }
}